package com.ds.robot.assistant.component;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.ReadConverterContext;
import com.alibaba.excel.converters.WriteConverterContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.util.DateUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;

@Slf4j
public class StringDateConverter implements Converter<Date> {
    @Override
    public Class<?> supportJavaTypeKey() {
        return Date.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.NUMBER;
    }

    @Override
    public Date convertToJavaData(ReadConverterContext<?> context) throws Exception {
        double value = context.getReadCellData().getNumberValue().doubleValue();
        return org.apache.poi.ss.usermodel.DateUtil.getJavaDate(value);
    }

    @Override
    public WriteCellData<?> convertToExcelData(WriteConverterContext<Date> context) throws Exception {
        return new WriteCellData<>(DateUtils.format(context.getValue(), "yyyy-MM-dd"));
    }
}
